package com.mspbots.core.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * com.mspbots.sync.config.RabbitConfigurer
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/29
 */
@Configuration
@RequiredArgsConstructor
public class RabbitConfiguration {

    private final AmqpAdmin amqpAdmin;

    @Bean
    public void declareTeamsSync() {
        Exchange exchange = ExchangeBuilder.fanoutExchange("msp.teams.sync").build();
        this.amqpAdmin.declareExchange(exchange);

        Queue queueTeamsUsers = new Queue("teams.users");
        this.amqpAdmin.declareQueue(queueTeamsUsers);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueTeamsUsers)
                .to(exchange).with("teams-sync-users").noargs());

    }

    @Bean
    public void declareEvent() {
        //reject ticket
        Exchange exchange = ExchangeBuilder.directExchange("next-ticket.trigger.event").build();
        this.amqpAdmin.declareExchange(exchange);

        Queue queue = new Queue("next-ticket.trigger.reject");
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("next-ticket-trigger-reject").noargs());

        //configuration
        queue = new Queue("next-ticket.redis.configuration");
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("next-ticket-redis-configuration").noargs());
    }

    @Bean
    public void declareTenantDelete() {
        Exchange exchange = ExchangeBuilder.fanoutExchange("msp.tenant.delete").build();
        this.amqpAdmin.declareExchange(exchange);
        Queue queueTeamsUsers = new Queue("tenant.delete");
        this.amqpAdmin.declareQueue(queueTeamsUsers);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueTeamsUsers)
                .to(exchange).with("tenant-delete-event").noargs());

    }


    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }

}
